﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Proggy.Web.Cms.Core.Models;
using Proggy.Web.Cms.AppHelpers;
using System.Threading;

namespace Proggy.Web.Cms.Core.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    public class DragAndDropManager
    {
        /// <summary>
        /// Set at app start up
        /// </summary>
        public static List<DragAndDropPartElement> DragAndDropPartsCache { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        public DragAndDropManager()
        {

        }

        /// <summary>
        /// The web.config section
        /// </summary>
        private DragAndDropPartsConfigurationSection _section;

        /// <summary>
        /// Parts
        /// </summary>
        private DragAndDropPartElementCollection _parts;

        /// <summary>
        /// Sets DragAndDropPartsCache
        /// </summary>
        public void SetCache()
        {
            // get section
            var section = this.DragAndDropPartsConfigurationSection;

            // get parts
            var parts = this.DragAndDropPartsElementCollection;

            // set as list
            DragAndDropPartsCache = parts.Cast<DragAndDropPartElement>().ToList();
        }

        /// <summary>
        /// The config section object
        /// </summary>
        public DragAndDropPartsConfigurationSection DragAndDropPartsConfigurationSection
        {
            get
            {
                if (this._section == null)
                {
                    this._section = (DragAndDropPartsConfigurationSection)
                        ConfigurationManager.GetSection("DragAndDropParts");
                }

                return this._section;
            }
            set
            {
                this._section = value;
            }
        }

        /// <summary>
        /// The parts elelement collection
        /// </summary>
        public DragAndDropPartElementCollection DragAndDropPartsElementCollection
        {
            get
            {
                if (this._parts == null)
                {
                    this._parts = this._section.AvailableDragAndDropParts;
                }

                return this._parts;
            }
            set
            {
                this._parts = value;
            }
        }

        public static void AddOrOverwriteToAddedPartsCache(Models.AddedDragAndDropItem dragAndDropItem)
        {
            // try find
            var found = AddedPartsCache.SingleOrDefault(x => x.UniqueId == dragAndDropItem.UniqueId && x.NodeId == dragAndDropItem.NodeId);

            // check
            if (found != null)
            {
                AddedPartsCache.Remove(found);
            }

            // add
            AddedPartsCache.Add(dragAndDropItem);
        }

        public static AddedDragAndDropItem GetFromAddedPartsCache(string part)
        {
            return AddedPartsCache.SingleOrDefault(x => x.UniqueId == part);
        }

        private static List<AddedDragAndDropItem> _addedPartsCache;
        public static List<AddedDragAndDropItem> AddedPartsCache
        {
            get
            {
                if (_addedPartsCache == null)
                {
                    _addedPartsCache = new List<AddedDragAndDropItem>();
                }
                return _addedPartsCache;
            }
        }

        public static void RemoveAddedPartsForNodeId(int nodeId)
        {
            // remove
            DragAndDropManager.AddedPartsCache.RemoveAll(x => x.NodeId == nodeId);

            // get now
            var now = DateTime.Now;

            // check time and refresh if required
            if (_addedPartsLastRefreshed != null)
            {

                // check time comparison
                if (_addedPartsLastRefreshed.AddHours(1) < now)
                {

                    // refresh parts cache
                    _addedPartsCache = new List<AddedDragAndDropItem>();
                    _addedPartsLastRefreshed = now;
                }

            }
            else
            {
                // refresh time
                _addedPartsLastRefreshed = now;
            }
        }

        private static DateTime _addedPartsLastRefreshed;
    }
}
